package com.ruoyi.mongo.util;

import java.util.ArrayList;
import java.util.List;
import org.bson.Document;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.ruoyi.mongo.souce.MongoDB04AuthPoolUtil;

/**
 * @Description: mongo管道操作
 * @ClassName: MongoTest05Aggergate
 * @author syc
 * @date: 2022年5月26日 上午10:46:43
 */
public class MongoTest05Aggergate {
	
	public static void main(String[] args) {
		MongoTest05Aggergate doc = new MongoTest05Aggergate();
		// doc.selectDocumentAggregateCount(); // 01
	    // doc.selectDocumentAggregateSum(); // 02
		// doc.selectDocumentAggregateGroupBySum(); // 03
		// doc.selectDocumentAggregateGroupByWhere(); // 04
		doc.selectDocumentAggregateGroupByHaving(); // 05
		
		
		
	}
	
	/**
	 * @Description:需求: 查询集合中的文档数量 db.test_user.aggregate([{$group:{_id:null,count:{$sum:1}}}])
	 * @author syc
	 * @date: 2022年5月26日 上午10:47:02
	 */
	public void selectDocumentAggregateCount() {
		MongoCollection<Document> collection = MongoDB04AuthPoolUtil.getCollection("test", "test_user");
		Document sum = new Document();
		sum.put("$sum", 1);

		Document count = new Document();
		count.put("_id", null);
		count.put("count", sum);

		Document group = new Document();
		group.put("$group", count);

		List<Document> list = new ArrayList<>();
		list.add(group);
		AggregateIterable<Document> iterable = collection.aggregate(list);
		MongoCursor<Document> cursor = iterable.iterator();
		while (cursor.hasNext()) {
			// 每掉一次next移动以下指针
			Document docu = cursor.next();
			System.out.println(docu.get("count"));
		}
	}

	/**
	 * @Description: 需求: 查询集合中所有size 键中的值的总和 db.test_user.aggregate([{$group:{_id:null,totalAge:{$sum:"$age"}}}])
	 * @author syc
	 * @date: 2022年5月26日 上午10:51:17
	 */
	public void selectDocumentAggregateSum() {
		MongoCollection<Document> collection = MongoDB04AuthPoolUtil.getCollection("test", "test_user");
		Document sum = new Document();
		sum.put("$sum", "$age");

		Document count = new Document();
		count.put("_id", null);
		count.put("totalAge", sum);

		Document group = new Document();
		group.put("$group", count);

		List<Document> list = new ArrayList<>();
		list.add(group);
		AggregateIterable<Document> iterable = collection.aggregate(list);
		MongoCursor<Document> cursor = iterable.iterator();
		while (cursor.hasNext()) {
			// 每掉一次next移动以下指针
			Document docu = cursor.next();
			System.out.println(docu.get("totalAge"));
		}
	}

	/**
	 * @Description: 需求: 对sex 进行分组,计算每组中的age的总和 
	 * @author syc
	 * @date: 2022年5月26日 上午10:54:37
	 * db.test_user.aggregate([{$group:{_id:"$sex",totalAge:{$sum:$age}}}])
	 */
	public void selectDocumentAggregateGroupBySum() {
		MongoCollection<Document> collection = MongoDB04AuthPoolUtil.getCollection("test", "test_user");
		Document sum = new Document();
		sum.put("$sum", "$age");

		Document count = new Document();
		count.put("_id", "$sex");
		count.put("totalAge", sum);

		Document group = new Document();
		group.put("$group", count);

		List<Document> list = new ArrayList<>();
		list.add(group);
		AggregateIterable<Document> iterable = collection.aggregate(list);
		MongoCursor<Document> cursor = iterable.iterator();
		while (cursor.hasNext()) {
			// 每掉一次next移动以下指针
			Document docu = cursor.next();
			System.out.println(docu.get("totalAge"));
		}
	}

	/**
	 * @Description: 需求: 查询test_user 集合文档的age 大于 20.的数量
	 * @author syc
	 * @date: 2022年5月26日 上午10:56:59
	 * db.test_user.aggregate([{$match:{age:{$gt:20}}},{$group:{_id:null,totalSize:{$sum:1}}}])
	 */
	public void selectDocumentAggregateGroupByWhere() {
		MongoCollection<Document> collection = MongoDB04AuthPoolUtil.getCollection("test", "test_user");
		Document gt = new Document();
		gt.put("$gt", 20);
		Document size = new Document();
		size.put("age", gt);
		Document match = new Document();
		match.put("$match", size);

		Document sum = new Document();
		sum.put("$sum", 1);

		Document count = new Document();
		count.put("_id", "$title");
		count.put("totalSize", sum);

		Document group = new Document();
		group.put("$group", count);

		List<Document> list = new ArrayList<>();
		list.add(match);
		list.add(group);
		AggregateIterable<Document> iterable = collection.aggregate(list);
		MongoCursor<Document> cursor = iterable.iterator();
		while (cursor.hasNext()) {
			// 每掉一次next移动以下指针
			Document docu = cursor.next();
			System.out.println(docu.get("totalSize"));
		}
	}

	/**
	 * @Description: 查询dev 集合,根据sex分组计算每组的age的总和,并过滤掉总和小于200的文档
	 * @author syc
	 * @date: 2022年5月26日 上午11:04:42
	 * db.test_user.aggregate([{$group:{_id:"$sex",totalAge:{$sum:"$age"}}},{$match:{totalAge:{$gte,200}}}])
	 */
	public void selectDocumentAggregateGroupByHaving() {
		MongoCollection<Document> collection = MongoDB04AuthPoolUtil.getCollection("test", "test_user");
		Document sum = new Document();
		sum.put("$sum", "$age");

		Document totalSize = new Document();
		totalSize.put("_id", "$sex");
		totalSize.put("totalAge", sum);

		Document group = new Document();
		group.put("$group", totalSize);

		Document gt = new Document();
		gt.put("$gte", 200);
		Document mtotalSize = new Document();
		mtotalSize.put("totalAge", gt);
		Document match = new Document();
		match.put("$match", mtotalSize);
		List<Document> list = new ArrayList<>();
		list.add(group);
		list.add(match);

		AggregateIterable<Document> iterable = collection.aggregate(list);
		MongoCursor<Document> cursor = iterable.iterator();
		while (cursor.hasNext()) {
			// 每掉一次next移动以下指针
			Document docu = cursor.next();
			System.out.println(docu.get("totalAge"));
		}
	}

}
